//
//  mineController.m
//  Focus
//
//  Created by dlios on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "mineController.h"
#import "MineCell.h"
@interface mineController ()

@end

@implementation mineController
-(void)dealloc{
    [_arrayThree release];
    [_arrayTwo release];
    [_arrayOne release];
    [_table release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.arrayOne = [NSMutableArray array];
        self.arrayTwo = [NSMutableArray array];
        self.arrayThree = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:226/255.0 green:44/255.0 blue:56/255.0 alpha:1.0];
    
#pragma mark - 
#pragma mark - 创建View
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    title.text = @"登陆Focus体验更多功能";
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont systemFontOfSize:16];
    title.textAlignment =  NSTextAlignmentCenter;
    title.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = title;
    [title release];

#pragma mark -
#pragma mark - 创建导航栏左边button
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(0, 0, 40, 40);
    [left setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:left]autorelease];
#pragma mark - 
#pragma mark - 创建导航栏右边button
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 40, 40);

    [right setImage:[UIImage imageNamed:@"iconfont-qingchu.png"] forState:UIControlStateNormal];
    right.titleLabel.font = [UIFont systemFontOfSize:14];
    [right addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:right]autorelease];

#pragma mark -
#pragma mark - 创建table
    [self creatTable];
#pragma mark - 
#pragma mark - 注册cell
    [_table registerClass:[MineCell class] forCellReuseIdentifier:@"cell"];
#pragma mark -
#pragma mark - arrayOne
    self.arrayOne = [NSMutableArray arrayWithObjects:@"好友分享",@"我的消息",@"推送资讯",@"积分活动",@"我的收藏", nil];
#pragma mark - 
#pragma mark - arrayTwo
    self.arrayTwo = [NSMutableArray arrayWithObjects:@"离线下载",@"夜间模式",@"更多设置", nil];
#pragma mark - 
#pragma mark - arrayThree
    self.arrayThree = [NSMutableArray arrayWithObjects:@"应用推荐",@"意见反馈",@"关于FOCUS", nil];
    
}
#pragma mark -
#pragma mark - 创建tableview
-(void)creatTable{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    [_table release];
    
}
#pragma mark - 
#pragma mark -必须实现的两个协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return [self.arrayOne count];
    }else if (section == 1){
        return [self.arrayTwo count];
    }else if (section == 2){
        return [self.arrayThree count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.myImage setImage:[UIImage imageNamed:@"jiantou.png"]];
    if (indexPath.section == 0) {
        cell.title.text = [self.arrayOne objectAtIndex:indexPath.row];
    }else if (indexPath.section == 1 || indexPath.row == 1){
        cell.title.text = [self.arrayTwo objectAtIndex:indexPath.row];
        
    }else{
        cell.title.text = [self.arrayThree objectAtIndex:indexPath.row];
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return @"我的功能";
    }else if (section == 2){
        return @"我的应用";
    }
    
    return nil;
}
#pragma mark - 
#pragma mark - 左边按钮点击事件
-(void)leftAction:(UIButton *)sender{
    
}
#pragma mark -
#pragma mark - 右边按钮点击事件
-(void)rightAction:(UIButton *)sender{
//清除缓存代码
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    
    NSString *str = [NSString stringWithFormat:@"当前缓存%.1fM", [self folderSizeAtPath:path]];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
    
//提示框
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确定清除缓存" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [alert show];
    [alert release];
    

}
//文件大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
//遍历所有文件
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}
//Alert点击事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIButton *button = (UIButton *)[self.view viewWithTag:5000];
    [button setTitle:@"清除缓存" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
